package csel.view;

import java.awt.Color;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import csel.model.Entity;
import csel.model.Observer;

/*
 * Reasons for Existence: Encapsulates the part of the status viewport that displays stats. 
 * This is a distinct function and should be differentiated from the other features of the StatusViewport.
 * 
 * @author Alex
 */
public class StatsViewer extends JPanel implements Observer
{
	private static final long serialVersionUID = 1L;
	
	JLabel lives;
	JLabel strength;
	JLabel agility;
	JLabel intellect;
	JLabel hardiness;
	JLabel experience;
	JLabel movement;
	JLabel level;
	JLabel life;
	JLabel mana;
	JLabel offensiveRating;
	JLabel defensiveRating;
	JLabel armorRating;

	JLabel livesV;
	JLabel strengthV;
	JLabel agilityV;
	JLabel intellectV;
	JLabel hardinessV;
	JLabel experienceV;
	JLabel movementV;
	JLabel levelV;
	JLabel lifeV;
	JLabel manaV;
	JLabel offensiveRatingV;
	JLabel defensiveRatingV;
	JLabel armorRatingV;

	JPanel labelsPane;
	JPanel valuesPane;

	public StatsViewer(Entity e)
	{	
		setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
		
		Color color = Color.WHITE;

		labelsPane = new JPanel();
		valuesPane = new JPanel();

		labelsPane.setLayout(new BoxLayout(labelsPane,BoxLayout.Y_AXIS));
		valuesPane.setLayout(new BoxLayout(valuesPane,BoxLayout.Y_AXIS));

		labelsPane.removeAll();
		valuesPane.removeAll();
		
		labelsPane.add(createLabel("Lives Left:", color));
		labelsPane.add(createLabel("Strength:" , color));
		labelsPane.add(createLabel("Agility:" , color));
		labelsPane.add(createLabel("Intellect:" , color));
		labelsPane.add(createLabel("Hardiness:" , color));
		labelsPane.add(createLabel("Experience:       " , color));
		labelsPane.add(createLabel("Movement:" , color));
		labelsPane.add(createLabel("Level:" , color));
		labelsPane.add(createLabel("Life:" , color));
		labelsPane.add(createLabel("Mana:" , color));
		labelsPane.add(createLabel("Offense:" , color));
		labelsPane.add(createLabel("Defense:" , color));
		labelsPane.add(createLabel("Armor:" , color));

		livesV = createLabel(Integer.toString(e.getLives()), color);
		strengthV = createLabel(Integer.toString(e.getStrength()), color);
		agilityV = createLabel(Integer.toString(e.getAgility()), color);
		intellectV = createLabel(Integer.toString(e.getIntellect()), color);
		hardinessV = createLabel(Integer.toString(e.getHardiness()), color);
		experienceV = createLabel(Integer.toString(e.getExperience()), color);
		movementV = createLabel(Integer.toString(e.getSpeed()), color);
		levelV = createLabel(Integer.toString(e.getLevel()), color);
		lifeV = createLabel(Integer.toString(e.getLife()), color);
		manaV = createLabel(Integer.toString(e.getMana()), color);
		offensiveRatingV = createLabel(Integer.toString(e.getOffense()), color);
		defensiveRatingV = createLabel(Integer.toString(e.getDefense()), color);
		armorRatingV = createLabel(Integer.toString(e.getArmor()), color);
		
		valuesPane.add(livesV);
		valuesPane.add(strengthV);
		valuesPane.add(agilityV);
		valuesPane.add(intellectV);
		valuesPane.add(hardinessV);
		valuesPane.add(experienceV);
		valuesPane.add(movementV);
		valuesPane.add(levelV);
		valuesPane.add(lifeV);
		valuesPane.add(manaV);
		valuesPane.add(offensiveRatingV);
		valuesPane.add(defensiveRatingV);
		valuesPane.add(armorRatingV);

		revalidate();
		//repaint();

		labelsPane.setOpaque(false);
		valuesPane.setOpaque(false);

		modelUpdate(e);

		add(labelsPane);
		add(valuesPane);

		e.addObserver(this);
	}

	public void setEntity(Entity e)
	{
		e.addObserver(this);
		modelUpdate(e);
	}

	public void modelUpdate(Object o)
	{
		Entity e = ((Entity) o);

		livesV.setText(Integer.toString(e.getLives()));
		strengthV.setText(Integer.toString(e.getStrength()));
		agilityV.setText(Integer.toString(e.getAgility()));
		intellectV.setText(Integer.toString(e.getIntellect()));
		hardinessV.setText(Integer.toString(e.getHardiness()));
		experienceV.setText(Integer.toString(e.getExperience()));
		movementV.setText(Integer.toString(e.getSpeed()));
		levelV.setText(Integer.toString(e.getLevel()));
		lifeV.setText(Integer.toString(e.getLife()));
		manaV.setText(Integer.toString(e.getMana()));
		offensiveRatingV.setText(Integer.toString(e.getOffense()));
		defensiveRatingV.setText(Integer.toString(e.getDefense()));
		armorRatingV.setText(Integer.toString(e.getArmor()));

		revalidate();
		repaint();
	}
	private JLabel createLabel(String labelName, Color color)
	{
		JLabel tempLabel = new JLabel(labelName);
		tempLabel.setForeground(color);
		return tempLabel;
	}
	
	private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
	{
		throw new java.io.NotSerializableException("AvatarViewer should not be serialized.");
	}
	
	private void readObject(java.io.ObjectInputStream in) throws java.io.IOException
	{
		throw new java.io.NotSerializableException("AvatarViewer should not be serialized.");
	}
}
